// component/component/uploadImage/uploadImage.js
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    maxImgLength:{
      type:Number,
      value:9,
    },
    imgArray:{
      type:Array,
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    imgArray:[]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    
    addImg: function (e) {
      let that = this;
      wx.chooseImage({
        count: that.data.maxImgLength-that.data.imgArray.length,
        sizeType: ['compressed'],
        sourceType: ['album', 'camera'],
        success: function (res) {
          for (let i = 0; i < res.tempFilePaths.length; i++) {
            that.data.imgArray.push(res.tempFilePaths[i])
          }
          that.setData({
            imgArray: that.data.imgArray
          })
          that.triggerEvent('setimgarr',that.data.imgArray);

        }
      })
    },
    /**
     * 删除图片
     */
    deleteImg: function (e) {
      let that = this
      let deleteIndex = e.currentTarget.dataset.index
      that.data.imgArray.splice(deleteIndex, 1)
      that.setData({
        imgArray: that.data.imgArray
      })
      that.triggerEvent('setimgarr',that.data.imgArray);
    },
    
    /**
     * 图片预览
     */
    preViewImgs: function (e) {
      let current = e.currentTarget.dataset.current;
      let urls = e.currentTarget.dataset.urls;
      wx.previewImage({
        current: current, // 当前显示图片的http链接
        urls: urls // 需要预览的图片http链接列表
      })
    },
  }
})
